package com.dengbw.systemtransfer.mapper;

import com.dengbw.systemtransfer.interfaces.MyBaseMapper;
import com.dengbw.systemtransfer.model.FuncId;
import com.dengbw.systemtransfer.model.db.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author dengbw
 * @date 2021/11/2 4:53 下午
 */
@Mapper
public interface SysUserMapper extends MyBaseMapper<SysUser> {
    @Update("UPDATE sys_user ssu,(SELECT su.user_id AS uId, GROUP_CONCAT( cfc.id ORDER BY cfc.sort_no asc) AS funId " +
            "FROM sys_user su, cmm_function_cofig cfc " +
            "WHERE su.sub_tenant_id = cfc.sub_tenant_id " +
            "and su.sub_tenant_id = #{subId} " +
            "AND cfc.user_type = 3 AND cfc.is_mr = 1 GROUP BY su.user_id ) temp SET ssu.nh_function = temp.funId WHERE ssu.user_id = temp.uId")
    int updateNonghu(Long subId);

    @Update("UPDATE sys_user ssu,( SELECT su.user_id AS uId, GROUP_CONCAT( cfc.id ORDER BY cfc.sort_no asc) AS funId " +
            "FROM sys_user su, cmm_function_cofig cfc " +
            "WHERE su.sub_tenant_id = cfc.sub_tenant_id " +
            "and su.sub_tenant_id = #{subId} " +
            "AND cfc.user_type = 2 AND cfc.is_mr = 1 GROUP BY su.user_id ) temp SET ssu.gb_function = temp.funId WHERE ssu.user_id = temp.uId;")
    int updateFuwu(Long subId);

    @Select("SELECT su.user_id AS uid, GROUP_CONCAT( cfc.id ORDER BY cfc.sort_no asc) AS funid FROM sys_user su, cmm_function_cofig cfc WHERE su.sub_tenant_id = cfc.sub_tenant_id and su.sub_tenant_id = #{subId} AND cfc.user_type = 3 AND cfc.is_mr = 1 GROUP BY su.user_id")
    List<FuncId> findNonghu(Long subId);

    @Select("SELECT su.user_id AS uid, GROUP_CONCAT( cfc.id ORDER BY cfc.sort_no asc) AS funid FROM sys_user su, cmm_function_cofig cfc WHERE su.sub_tenant_id = cfc.sub_tenant_id and su.sub_tenant_id = #{subId} AND cfc.user_type = 2 AND cfc.is_mr = 1 GROUP BY su.user_id")
    List<FuncId> findFuwu(Long subId);
}
